﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ToDoBasket.Model;

namespace ToDoBasket.Custom
{
    [TemplatePart(Name = "gdDesciption", Type = typeof(Grid))]
    //[TemplatePart(Name = "tbDesciption", Type = typeof(Label))]
    [TemplatePart(Name = "tbOriginal", Type = typeof(TextBox))]
    [TemplatePart(Name = "tbPrefix", Type = typeof(TextBlock))]
    [TemplatePart(Name = "btnMove", Type = typeof(Button))]
    [TemplatePart(Name = "btnDel", Type = typeof(Button))]
    public class PotatoItem : Control
    {
        //注册事件
        public static readonly RoutedEvent BtnDeleteClickEvent = EventManager.RegisterRoutedEvent("BtnDeleteClick",
            RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(PotatoItem));
        public static readonly RoutedEvent BtnPrefixClickEvent = EventManager.RegisterRoutedEvent("BtnPrefixClick",
            RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(PotatoItem));
        public static readonly RoutedEvent MoveStartEvent = EventManager.RegisterRoutedEvent("MoveStart",
            RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(PotatoItem));
        //事件处理器属性
        public event RoutedEventHandler BtnDeleteClick
        {
            add { AddHandler(BtnDeleteClickEvent, value); }
            remove { RemoveHandler(BtnDeleteClickEvent, value); }
        }
        public event RoutedEventHandler BtnPrefixClick
        {
            add { AddHandler(BtnPrefixClickEvent, value); }
            remove { RemoveHandler(BtnPrefixClickEvent, value); }
        }
        public event RoutedEventHandler MoveStart
        {
            add { AddHandler(MoveStartEvent, value); }
            remove { RemoveHandler(MoveStartEvent, value); }
        }
        //构造函数
        static PotatoItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(PotatoItem), new FrameworkPropertyMetadata(typeof(PotatoItem)));
        }

        private TextBox tbOriginal;
        private TextBlock tbPrefix;
        private Grid gdDesciption;
        //private TextBlock tbDesciption;
        private Button btnMove;
        private Button btnDel;

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            tbOriginal = Template.FindName("tbOriginal", this) as TextBox;
            tbPrefix = Template.FindName("tbPrefix", this) as TextBlock;
            gdDesciption = Template.FindName("gdDesciption", this) as Grid;
            //tbDesciption = Template.FindName("tbDesciption", this) as TextBlock;
            btnMove = Template.FindName("btnMove", this) as Button;
            btnDel = Template.FindName("btnDel", this) as Button;

            //按下回车或失去焦点，完成编辑
            if (tbOriginal != null)
            {
                tbOriginal.LostFocus += (s, e) => EditOff();
                tbOriginal.KeyDown += (s, e) => { if (e.Key == Key.Enter)EditOff(); };
            }

            //单击前缀（标签）
            if (tbPrefix != null)
                tbPrefix.MouseLeftButtonUp += (s, e) => RaiseEvent(new RoutedEventArgs(BtnPrefixClickEvent, this));

            //双击编辑
            /*if (tbDesciption != null)
                tbDesciption.MouseDoubleClick += (s, e) => EditOn();*/
            if (gdDesciption != null)
                gdDesciption.MouseDown += (s, e) =>{if (e.ClickCount == 2) EditOn();};

            //按住移动
            if (btnMove != null)
                btnMove.PreviewMouseLeftButtonDown += (s, e) => RaiseEvent(new RoutedEventArgs(MoveStartEvent, this));

            //单击删除按钮
            if (btnDel != null)
                btnDel.Click += (s, e) => RaiseEvent(new RoutedEventArgs(BtnDeleteClickEvent, this));
        }

        //开始编辑
        void EditOn()
        {
            tbOriginal.Visibility = Visibility.Visible;
            Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() => tbOriginal.Focus()));
        }

        //完成编辑
        void EditOff()
        {
            tbOriginal.Visibility = Visibility.Collapsed;
            BindingExpression binding = tbOriginal.GetBindingExpression(TextBox.TextProperty);
            if (binding == null) return;
            if (Potato.IsValid(tbOriginal.Text))
                binding.UpdateSource();
            else
                binding.UpdateTarget();
        }
    }
}
